package org.oexam.ostudy.serv;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.oexam.ostudy.facade.CreateUserPage;
import org.oexam.ostudy.facade.struct.InsertUserDetails;
import org.oexam.ostudy.facade.struct.LoggedUserDetails;

/**
 * Servlet implementation class CreateUser
 */
@WebServlet("/CreateUser")
public class CreateUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CreateUser() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}
		if (details.isAuthenticated() && (details.getUserType() == 0)) {

			response.sendRedirect("CreateUser.jsp");

		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	// TODO Pradeep : Use this method to post/submit the fields when creating
	// the user.
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}
		if (details.isAuthenticated() && (details.getUserType() == 0)) {

			InsertUserDetails userDetail = new InsertUserDetails();
			
			userDetail.setUserName(request.getParameter("userName"));
			userDetail.setPassword(request.getParameter("password"));
			userDetail.setFirstName(request.getParameter("firstName"));
			userDetail.setLastName(request.getParameter("lastName"));
			userDetail.setUserType(request.getParameter("userType"));
			userDetail.setAddress1(request.getParameter("address1"));
			userDetail.setAddress2(request.getParameter("address2"));
			userDetail.setPhoneNumber(request.getParameter("phoneNumber"));
			userDetail.setEmailId(request.getParameter("emailAddress"));

			CreateUserPage create = new CreateUserPage();
			String retString = create.createUser(userDetail);

			if (retString.equals("")) {
				request.setAttribute("errMsg", "User Added Successfully");
				
				HttpSession session = request.getSession();
				session.setAttribute("message", "User Added Successfully");
				response.sendRedirect("Administrators.jsp");
				/*
				RequestDispatcher dispatcher = request.getRequestDispatcher("CreateUser.jsp");
				dispatcher.forward(request, response);
				*/
			} else {
				request.setAttribute("errMsg", retString);
				
				HttpSession session = request.getSession();
				session.setAttribute("message", retString);
				response.sendRedirect("CreateUser.jsp");
				
				/*
				RequestDispatcher dispatcher = request.getRequestDispatcher("CreateUser.jsp");
				dispatcher.forward(request, response);
				*/
			}

			

		}

	}

}
